package todolist.activities;

import java.util.List;

import todolist.activities.R;
import todolist.helpers.userDataSource;
import todolist.structures.User;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * Shows create new account screen
 *
 */
public class RegisterActivity extends Activity implements OnClickListener {
  
	userDataSource datasource;
	List<User> values;
	int found;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        datasource = new userDataSource(this);
		datasource.open();
        
        // Set View to register.xml
        setContentView(R.layout.register);
        
        TextView loginScreen = (TextView) findViewById(R.id.link_to_login);
        Button registerButton = (Button) findViewById(R.id.btnRegister);
        
        loginScreen.setOnClickListener(this);
        registerButton.setOnClickListener(this);
    }
	
    public void onClick(View v) {

    	switch(v.getId()){

    	case R.id.link_to_login:
    		// Switching to Login Screen/closing register screen
    		finish();
    		break;

    	case R.id.btnRegister: /** AlerDialog when click on Exit */
    		EditText name = (EditText) findViewById(R.id.reg_username);
    		EditText email = (EditText) findViewById(R.id.reg_email);
    		EditText password = (EditText) findViewById(R.id.reg_password);
		  
    		if(name.getText().toString().compareTo("")==0 || email.getText().toString().compareTo("")==0 || password.getText().toString().compareTo("")==0) {
    			Toast.makeText(RegisterActivity.this, "Please fill out all information",
					Toast.LENGTH_LONG).show();
    		}
	      
    		else {
			  
    			/*
			   * Still needs to support has functionality
			   */
    			values = datasource.getAllUsers();
    			
    			
    			//check to see if this is the first item added to db
    			if(values.size()==0){
    				datasource.createUser(name.getText().toString(), password.getText().toString());
	    			Toast.makeText(RegisterActivity.this, "Account has been created",
						Toast.LENGTH_LONG).show();
	    			finish(); 	
    			}
    			
    			//if its not, check for duplicates/add
    			
    			else {
    				for(int i=0; i<values.size(); i++){
        				//check to see if username exists
    					if(values.get(i).getName().compareTo(name.getText().toString())==0) {
    						found=1;
    					}
        			}
    				
					if(found==1){
    					Toast.makeText(RegisterActivity.this, "Username already exists!",
								Toast.LENGTH_LONG).show();
    				}
    				else {
    					datasource.createUser(name.getText().toString(), password.getText().toString());
		    			Toast.makeText(RegisterActivity.this, "Account has been created",
							Toast.LENGTH_LONG).show();
		    			
		    			finish();
    				}
    			}
    		}
		  
    		break;
		
    	}    
    }
    
    public boolean checkForNull(EditText username, EditText email, EditText pw) {
    	if (username.getText().toString().compareTo("")==0 || email.getText().toString().compareTo("")==0 || pw.getText().toString().compareTo("")==0) {
    		return true;
    	} else {
    		return false;
    	}
    }
    
	@Override
	protected void onResume() {
		datasource.open();
		super.onResume();
	}

	@Override
	protected void onPause() {
		datasource.close();
		super.onPause();
	}
} 